/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.asu.cse360.client.action;

import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.client.ui.frame.AppFrame;
import edu.asu.cse360.client.util.AppConfiguration;
import edu.asu.cse360.client.util.CustomerConfiguration;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.model.CustomerSession;
import edu.asu.cse360.model.Table;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
/**
 * When the admin wants to switch to customer mode they select it from the jMenu and selects customer mode and this is the action listener to update the ui
 * @author nadimhoque
 */

public class EnterCustomerModeAction extends AbstractAction{
    public AppFrame frame;
    private CustomerConfiguration customerConfiguration = CustomerConfiguration.instance();

    public EnterCustomerModeAction(AppFrame frame){
        super("Enter Customer Mode");
        this.frame = frame;

    }

    public void actionPerformed(ActionEvent e) {
        Table table = getNextAvaliable();
        if(table == null){
             JOptionPane.showMessageDialog(null, "There are no avaliable tables");
        }
        else{

        table.setRestaurant(RestaurantContext.getRestaurant());
        RestaurantContext.setCustomerMode(true);
        RestaurantContext.setTable(table);
        CustomerSession customerSession = new CustomerSession();
        customerSession.setTable(table);
        RestaurantContext.setSession(customerSession);
        customerConfiguration.configure(frame);
        customerConfiguration.showMenuPanel();
        }
    }

    private Table getNextAvaliable() {
        return ServiceFactory.getTableService().getNextAvailable(RestaurantContext.getRestaurant());
    }

}
